﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace MedievalGameProject
{
    public class ActorManager : DrawableGameComponent
    {
        public Character localPlayer;
        public Character remotePlayer;

        private List<Actor> actors { get; set; }
        private List<Actor> actorsToAdd;
        private List<Actor> actorsToRemove;

        public ActorManager(Game game)
            : base(game)
        {
            actors = new List<Actor>();
            actorsToAdd = new List<Actor>();
            actorsToRemove = new List<Actor>();
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            foreach (Actor actor in actors)
            {
                actor.Update(gameTime);
            }

            foreach (Actor actor in actorsToAdd)
            {
                actors.Add(actor);
            }
            actorsToAdd.Clear();

            foreach (Actor actor in actorsToRemove)
            {
                actors.Remove(actor);
            }
            actorsToRemove.Clear();
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            foreach (Actor actor in actors)
            {
                actor.Draw(gameTime);
            }
        }

        public List<Actor> CheckCollision(Actor actor)
        {
            List<Actor> actorsHit = new List<Actor>();
            foreach (Actor act in actors)
            {
                //Dont check for collision against other projectiles
                //or against itself
                if (!(act is Projectile) && !act.Equals(actor))
                {
                    if (actor.boundingShapes.CheckCollision(act.boundingShapes))
                        actorsHit.Add(act);
                }
            }

            if (localPlayer.boundingShapes.CheckCollision(actor.boundingShapes))
                actorsHit.Add(localPlayer);

            if (remotePlayer != null && remotePlayer.boundingShapes.CheckCollision(actor.boundingShapes))
                actorsHit.Add(remotePlayer);

            return actorsHit;
        }

        public List<Actor> GetActorsInViewFrustum()
        {
            List<Actor> actorsInView = new List<Actor>();

            foreach (Actor act in actors)
            {
                if (act.boundingShapes.outerBoundingBox.Intersects(Game1.camera.frustum))
                    actorsInView.Add(act);
            }

            actorsInView.Sort();

            return actorsInView;
        }

        public void ScheduleAddActor(Actor actor)
        {
            actorsToAdd.Add(actor);
        }

        public void ScheduleRemoveActor(Actor actor)
        {
            actorsToRemove.Add(actor);
        }

        public void ScheduleRemoveAll()
        {
            foreach (Actor actor in actors)
                actorsToRemove.Add(actor);
        }
    }
}
